package stu.sup.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import stu.sup.entity.Coupon;
import stu.sup.entity.CouponExample;
import stu.sup.entity.UserCoupon;
import stu.sup.entity.UserCouponExample;
import stu.sup.mapper.CouponMapper;
import stu.sup.mapper.UserCouponMapper;
import stu.sup.service.IUserCouponService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HUAWEI on 2020/6/27.
 */
public class UserCouponServiceImpl implements IUserCouponService{
    @Autowired
    UserCouponMapper userCouponMapper;
    @Autowired
    CouponMapper couponMapper;
    @Override
    public int addCoupon(int UserId, int CouponId) {
        UserCoupon userCoupon=new UserCoupon(UserId,CouponId);
        userCouponMapper.insert(userCoupon);
        return userCouponMapper.insert(userCoupon);
    }

    @Override
    public int delateCoupon(int UserId, int CouponId) {
        UserCouponExample example=new UserCouponExample();
        UserCouponExample.Criteria criteria=example.createCriteria();
        criteria.andCouponIdEqualTo(CouponId);
        criteria.andUserIdEqualTo(UserId);
        return userCouponMapper.deleteByExample(example);
    }

    @Override
    public List<Coupon> selecthave(int UserId) {
        List<Coupon> coupons=null;
        UserCouponExample example=new UserCouponExample();
        UserCouponExample.Criteria criteria=example.createCriteria();
        criteria.andUserIdEqualTo(UserId);
        List<UserCoupon> userCoupons=userCouponMapper.selectByExample(example);
        for (UserCoupon c:userCoupons){
            Coupon coupon=couponMapper.selectByPrimaryKey(c.getCouponId());
            coupons.add(coupon);
        }
        return coupons;
    }
}
